#!/usr/bin/env perl
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is Komodo code.
# 
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
# 
# Contributor(s):
#   ActiveState Software Inc
# 
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
# 
# ***** END LICENSE BLOCK *****

Import(
    'cons',
    'build',
    'buildAbsDir',
    'compiler',
    'platform',
    'buildFlavour',
    'buildType',
    'idlExportDir',
    'komodoPythonUtilsDir',
    'supportDir',
    'unsiloedPythonExe',
    'productType',
    'mozVersion',
    'mozExtensionDir',
    'mozChromeDir',
    'komodoShortVersion',
    'mozBin',
    'withJSLib',
    'ranRegxpcomStateFileName',
    'siloedPython',
    'readmeDir',
    'sdkDir',
    'universal',
    'komodoDevDir',
    'withKomodoCix',
    'withWatchdogFSNotifications',
);

# Various setup.py-based things need extra flags for debbugging
if ($buildType eq 'debug') {
    $debugFlags = "--debug";
} else {
    $debugFlags = "";
}

my $consLocal = $cons;

#---- Komodo's schema-based autocomplete and other parts of the
#     codeintel system use ElementTree.
# Python ElementTree (cElementTree depends upon this)
my $landmark = "$komodoPythonUtilsDir/elementtree/__init__.py";
$consLocal->Command($landmark,
               "elementtree/setup.py",
               qq(
                cd %1:d && $siloedPython %1:f build $debugFlags
                cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
               ));
$consLocal->DependsRecursive($landmark, "elementtree", ("build", '\.svn'));
# cElementTree
my $pydExt = ($platform eq "win" ? ".pyd" : ".so");
my $landmark = "$komodoPythonUtilsDir/cElementTree$pydExt";
$consLocal->Command($landmark,
                    "cElementTree/setup.py",
                    qq(
                     cd %1:d && $siloedPython %1:f build $debugFlags
                     cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
                    ));
$consLocal->DependsRecursive($landmark, "cElementTree", ("build", '\.svn'));

#---- sgmlop (used for HTML parsing for HTML autocomplete)
my $pydExt = ($platform eq "win" ? ".pyd" : ".so");
my $landmark = "$komodoPythonUtilsDir/sgmlop$pydExt";
$consLocal->Command($landmark,
               "sgmlop/setup.py",
               qq(
                cd %1:d && $siloedPython %1:f build $debugFlags
                cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
               ));
$consLocal->DependsRecursive($landmark, "sgmlop", ("build", '\.svn'));


#---- Stock XML catalogs
$consLocal->InstallRecursive("$supportDir/catalogs",
                        #XXX must be absolute path (bug in InstallRecursive)
                        cwd()."/contrib/catalogs",
                        ('\.svn'));


#---- Komodo's SFTP support uses 'paramiko' which, in turn, uses PyCrypto and
#     ecdsa.
# PyCrypto (http://www.amk.ca/python/code/crypto)
my $landmark = "$komodoPythonUtilsDir/Crypto/__init__.py";
$consLocal->Command($landmark,
               "pycrypto/setup.py",
               qq(
                cd %1:d && $siloedPython %1:f build $debugFlags
                cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
               ));
$consLocal->DependsRecursive($landmark, "pycrypto", ("build", '\.svn'));

# ecdsa (http://github.com/warner/python-ecdsa)
my $landmark = "$komodoPythonUtilsDir/ecdsa";
$consLocal->InstallRecursive($landmark,
                        #XXX must be absolute path (bug in InstallRecursive)
                        cwd()."/contrib/ecdsa/ecdsa");

# paramiko (http://www.lag.net/paramiko/)
# See bug 73155 for a long discussion on why we are NOT using
# paramiko's setup.py for installation.
$landmark = "$komodoPythonUtilsDir/paramiko";
$consLocal->InstallRecursive($landmark,
                        #XXX must be absolute path (bug in InstallRecursive)
                        cwd()."/contrib/paramiko/paramiko",
                        ('\.svn'));



if ($withJSLib) {
    #---- jslib
    # Some of Komodo's optional extensions use jslib:
    #   komodo_javascript_debugger (Komodo's FF extension for JS debugging)
    #   Casper (a Komodo extension for GUI testing)
    #
    # To update jslib, do the following *on a Linux box* to avoid EOL issues.
    #
    #   cd contrib/jslib
    #   rm -rf *
    #   wget http://www.mozdevgroup.com/dropbox/jslib/signed/jslib_current_signed.xpi
    #   unzip jslib_current_signed.xpi
    #   rm -f jslib_current_signed.xpi
    #   patch -p0 < patches/jslib_komodo_rdf.patch
    #   px diff -sn ./... | px -x - add
    #   p4 diff -se ./... | p4 -x - edit
    #   p4 diff -sd ./... | p4 -x - delete
    #   p4 submit ./...
    #       Update to jsLib version X.X.XXX
    #
    # Currently we install the extension for dev builds and only place
    # the .xpi in the install tree for installer builds because jslib is
    # only required for dev testing bits (e.g. Casper).

    if ($buildFlavour eq "dev") {
        # install into komodo dev build
        my $baseDir = "$mozExtensionDir/{DF8E5247-8E0A-4de6-B393-0735A39DFD80}/";
        $consLocal->InstallRecursive(
            $baseDir,
            #XXX must be absolute path (bug in InstallRecursive)
            cwd()."/contrib/jslib",
            (".consign", "jslib.xpi", '\.svn'));

        # install into firefox
        # XXX FIXME The following installrecursive does not work.  why?
        #     Answer:
        #     1. Because $firefoxProfileDir isn't defined.
        #     2. Because Cons is only considering targets under the dirs
        #        it is told to build via Default() call in Construct --
        #        and the firefox profile dir is not in these dirs.
        #$baseDir = "$firefoxProfileDir/extensions/{DF8E5247-8E0A-4de6-B393-0735A39DFD80}/";
        #if (! -d $baseDir) {
        #    mkdir($baseDir);
        #}
        #$cons->Install($baseDir, "jslib/install.rdf");
        #$cons->InstallRecursive($baseDir,
        #                        cwd()."/build/release/contrib/jslib/",
        #                        (".consign", "install.p.rdf", "jslib.xpi", '\.svn'));
    }

    $consLocal->Command(
        "jslib/jslib.xpi",
        "jslib/install.rdf",
        qq(
            $unsiloedPythonExe bin/run-in-dir.py %1:d zip -r %0:f "*" -x .consign chrome/.consign chrome/jsliblive/.consign components/.consign META-INF/.consign
        ));
    $consLocal->DependsRecursive("jslib/jslib.xpi", "jslib", (".consign", '\.svn'));
    $consLocal->Install("$supportDir/modules", 'jslib/jslib.xpi');
}


#---- PCRE
# A C-based regex engine being used for Komodo's multi-language lexer (aka
# luddite, aka LexUDT). Note that the code here is integrated from
#   //depot/main/support/pcre/...
# and the original pcre source integrated from:
#   //depot/main/contrib/pcre/...
# and that from:
#   ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
# also here:
#   \\crimper\apps\Komodo\support\pcre

# Build a static PCRE lib.
if ($platform eq "win") {
    # We are currently building all Komodo bits with VC6 so we use msdev.com.

    my $scintilla_dir_for_cons = "$build/scintilla";
    my $scintilla_dir = "$buildAbsDir\\scintilla";
    my $silvercity_dir_for_cons = "$build/silvercity";
    my $silvercity_dir = "$buildAbsDir\\silvercity";

    $landmark = "$scintilla_dir_for_cons/win32/libpcre.lib";
    # Building with the .mak files here should work with any VC
    # version.
    $consLocal->Command([$landmark,
                    "$scintilla_dir_for_cons/include/pcre.h",
                    "$silvercity_dir_for_cons/libpcre.lib"],
                   "pcre/pcreposix.h",
                   "pcre/Win32/dftables.mak",
                   "pcre/Win32/libpcre.mak",
                   qq(
                    cd %2:d && nmake -nologo -f %2:f
                    cd %3:d && nmake -nologo -f %3:f
                    copy %1:d\\Win32\\Release\\libpcre.lib $scintilla_dir\\win32\\libpcre.lib
                    copy %1:d\\Win32\\pcre.h $scintilla_dir\\include\\pcre.h
                    copy %1:d\\Win32\\Release\\libpcre.lib $silvercity_dir\\libpcre.lib
                   ));
    $consLocal->DependsRecursive($landmark, "pcre",
                            ('\.svn', 'doc', 'testdata'));

} else {
    my $scintilla_dir_for_cons = "$build/scintilla";
    my $scintilla_dir = "$buildAbsDir/scintilla";
    my $silvercity_dir_for_cons = "$build/silvercity";
    my $silvercity_dir = "$buildAbsDir/silvercity";
    my $build_env = "";

    my $scintilla_plat_dir = "gtk";
    if ($platform eq "darwin") {
        $scintilla_plat_dir = "cocoa";
    }

    $landmark = "$scintilla_dir_for_cons/$scintilla_plat_dir/libpcre.a";
    $consLocal->Command([$landmark,
                    "$scintilla_dir_for_cons/include/pcre.h",
                    "$silvercity_dir_for_cons/libpcre.a"],
                   "pcre/configure",

                   # 'ranlib' required on Mac OS X for SilverCity build
                   # to avoid this error:
                   #    ld: table of contents for archive: libpcre.a is out of date; rerun ranlib(1) (can't load from it)
                   #    error: command 'c++' failed with exit status 1
                   # See this discussion:
                   #    http://lists.apple.com/archives/darwin-dev/2005/Sep/msg00053.html
                   qq(
                    cd %1:d && ./configure --enable-shared=no --enable-utf8 --enable-unicode-properties $build_env && make
                    ranlib %1:d/.libs/libpcre.a
                    cp %1:d/.libs/libpcre.a $scintilla_dir/$scintilla_plat_dir/libpcre.a
                    cp %1:d/pcre.h $scintilla_dir/include/pcre.h
                    cp %1:d/.libs/libpcre.a $silvercity_dir/libpcre.a
                    ranlib $silvercity_dir/libpcre.a
                   ));
    # Needs depends recursive for all three landmark files!
    $consLocal->DependsRecursive([$landmark,
                                  "$scintilla_dir_for_cons/include/pcre.h",
                                  "$silvercity_dir_for_cons/libpcre.a"],
                                 "pcre",
                                 ('\.svn', 'doc', 'testdata'));
}


#---- pywin
# Komodo uses the wnd package (from http://wnd.sourceforge.net/)
# as a replacement for PyWin32. 'wnd' provides some of the same Windows
# API functionality via ctypes. We've renamed 'wnd' to 'pywin' to
# hopefully be more clear -- though that renaming may be reverted.

if ($platform eq "win") {
    my $landmark = "$komodoPythonUtilsDir/wnd";
    $consLocal->InstallRecursive($landmark,
                            cwd() . "/contrib/pywin/wnd-0-1-20",
                            ('\.svn', '\.consign'));
}



#---- inflector: Komodo's ruby codeintel uses this
# svn export http://opensvn.csie.org/inflector/trunk/python inflector
# 
# This is a Python package providing English word inflections similar to
# what Ruby on Rails does for naming db tables, model classes, etc. It
# is used for providing rails completion.
#
# Note: Not using InstallRecursive because don't want
# inflector/tests.py.
$consLocal->InstallPythonUtility("inflector/__init__.py", "inflector");
$consLocal->InstallPythonUtility("inflector/Inflector.py", "inflector");
$consLocal->InstallPythonUtility("inflector/Rules/__init__.py", "inflector/Rules");
$consLocal->InstallPythonUtility("inflector/Rules/Base.py", "inflector/Rules");
$consLocal->InstallPythonUtility("inflector/Rules/English.py", "inflector/Rules");
$consLocal->InstallPythonUtility("inflector/Rules/Spanish.py", "inflector/Rules");


#---- htmltidy 
{
local $build = substr($build, 1, length($build)); # drop the leading '#'
my ($exeName, $make, $makefile, $makeflags);
if ($platform eq "win") {
    $exeName = "tidy\\bin\\tidy.exe";
    $make = "nmake";
    $makefile = "Makefile.vc6";
    $tidyDir = "$build\\contrib\\tidy\\build\\msvc\\";
    # Get rid of -opt:ref -opt:nowin98, which is no longer supported on new compilers
    $makeflags = "LDNDEBUG=";
} else {
    $exeName = "tidy/bin/tidy";
    if ($platform eq "darwin") {
      $make = "RANLIB=ranlib make OTHERCFLAGS='-D_DEBUG=1 -mmacosx-version-min=10.5' ";
    } else {
      $make = "make";
    }
    $makefile = "Makefile";
    $tidyDir = "$build/contrib/tidy/build/gmake";
    $makeflags = "";
}


my %consTidy = $consLocal->copy();
$consTidy{CPPPATH} = "include";
$consTidy{LIBPATH} = "obj";
my $consTidy = new cons(%consTidy);

$consTidy->Command($exeName, 
                    "tidy/build/gmake/Makefile",
                    "tidy/build/msvc/Makefile.vc6",
                    "tidy/console/tab2space.c",
                    "tidy/console/tidy.c",
                    "tidy/include/buffio.h",
                    "tidy/include/platform.h",
                    "tidy/include/tidy.h",
                    "tidy/include/tidyenum.h",
                    "tidy/src/access.c",
                    "tidy/src/access.h",
                    "tidy/src/alloc.c",
                    "tidy/src/attrask.c",
                    "tidy/src/attrdict.c",
                    "tidy/src/attrdict.h",
                    "tidy/src/attrget.c",
                    "tidy/src/attrs.c",
                    "tidy/src/attrs.h",
                    "tidy/src/buffio.c",
                    "tidy/src/charsets.c",
                    "tidy/src/charsets.h",
                    "tidy/src/clean.c",
                    "tidy/src/clean.h",
                    "tidy/src/config.c",
                    "tidy/src/config.h",
                    "tidy/src/entities.c",
                    "tidy/src/entities.h",
                    "tidy/src/fileio.c",
                    "tidy/src/fileio.h",
                    "tidy/src/forward.h",
                    "tidy/src/iconvtc.c",
                    "tidy/src/iconvtc.h",
                    "tidy/src/istack.c",
                    "tidy/src/lexer.c",
                    "tidy/src/lexer.h",
                    "tidy/src/localize.c",
                    "tidy/src/mappedio.c",
                    "tidy/src/mappedio.h",
                    "tidy/src/message.h",
                    "tidy/src/parser.c",
                    "tidy/src/parser.h",
                    "tidy/src/pprint.c",
                    "tidy/src/pprint.h",
                    "tidy/src/streamio.c",
                    "tidy/src/streamio.h",
                    "tidy/src/tagask.c",
                    "tidy/src/tags.c",
                    "tidy/src/tags.h",
                    "tidy/src/tidy-int.h",
                    "tidy/src/tidylib.c",
                    "tidy/src/tmbstr.c",
                    "tidy/src/tmbstr.h",
                    "tidy/src/utf8.c",
                    "tidy/src/utf8.h",
                    "tidy/src/version.h",
                    "tidy/src/win32tc.c",
                    "tidy/src/win32tc.h",
                    "tidy/obj/empty.txt",
                    qq{cd $tidyDir && $make -f $makefile $makeflags}
                   );
$consTidy->Install("$supportDir/html", $exeName);
}

# six (required for html5lib)
$consLocal->Install("$komodoPythonUtilsDir", "six/six.py");

# html5lib
$landmark = "$komodoPythonUtilsDir/html5lib";
$consLocal->InstallRecursive($landmark,
                        #XXX must be absolute path (bug in InstallRecursive)
                        cwd()."/contrib/html5lib/html5lib",
                        ('\.svn'));

# jshint
$consLocal->Install("$supportDir/lint/javascript", "jslint/jshint.js");

# ejs
$consLocal->InstallAs("$mozChromeDir/komodo/content/library/ejs.js", "ejs/ejs_edited.js");

#less css
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/less.js", "smallstuff/less.js");

# commonjs
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/dot.js", "commonjs/dot.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/randomColor.js", "commonjs/randomColor.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/underscore.js", "commonjs/underscore.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/marked.js", "commonjs/marked.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/buffer.js", "commonjs/buffer.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/ieee754.js", "commonjs/ieee754.js");

# vscode-jsonrpc
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/cancellation.js", "vscode-jsonrpc/cancellation.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/events.js", "vscode-jsonrpc/events.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/is.js", "vscode-jsonrpc/is.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/linkedMap.js", "vscode-jsonrpc/linkedMap.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/main.js", "vscode-jsonrpc/main.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/messageReader.js", "vscode-jsonrpc/messageReader.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/messages.js", "vscode-jsonrpc/messages.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/messageWriter.js", "vscode-jsonrpc/messageWriter.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/thenable.js", "vscode-jsonrpc/thenable.js");
$consLocal->InstallAs("$mozChromeDir/komodo/content/contrib/commonjs/vscode-jsonrpc/License.txt", "vscode-jsonrpc/License.txt");


#----  Watchdog for file notifcations
if ($withWatchdogFSNotifications) {
    # Watchdog has further requirements: PyYAML, argh (and argparse for < 2.7)
    # and select-backport (for < 2.7). However, we don't use any of the features
    # that require those.
    
    my $landmark = "$komodoPythonUtilsDir/pathtools/__init__.py";
    $consLocal->Command($landmark,
                   "pathtools/setup.py",
                   qq(
                    cd %1:d && $siloedPython %1:f build $debugFlags
                    cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
                   ));
    $consLocal->DependsRecursive($landmark, "pathtools", ("build", '\.svn', '\.consign'));

    $landmark = "$komodoPythonUtilsDir/watchdog/__init__.py";
    $consLocal->Command($landmark,
                   "watchdog/setup.py",
                   qq(
                    cd %1:d && $siloedPython %1:f build $debugFlags
                    cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
                   ));
    $consLocal->DependsRecursive($landmark, "watchdog", ("build", '\.svn', '\.consign'));
}

#----  Zope property descriptors.
my $landmark = "$komodoPythonUtilsDir/zope/cachedescriptors/__init__.py";
$consLocal->Command($landmark,
               "zope/cachedescriptors/setup.py",
               qq(
                cd %1:d && $siloedPython %1:f build $debugFlags
                cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
               ));
$consLocal->DependsRecursive($landmark, "zope/cachedescriptors", ("build", '\.svn', '\.consign'));

#----  Scandir property descriptors.
my $landmark = "$komodoPythonUtilsDir/scandir/scandir.py";
$consLocal->Command($landmark,
               "scandir/setup.py",
               qq(
                cd %1:d && $siloedPython %1:f build $debugFlags
                cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
               ));
$consLocal->DependsRecursive($landmark, "scandir", ("build"));

#----  Markdown2.
$consLocal->InstallPythonUtility('smallstuff/markdown2.py');

if ($platform eq "win") {
    $consLocal->InstallPythonUtility('smallstuff/winprocess.py');
}

 
# Bugsnag
$consLocal->InstallRecursive("$komodoPythonUtilsDir/bugsnag",
                        #XXX must be absolute path (bug in InstallRecursive)
                        cwd()."/contrib/bugsnag-python/2.3.1");

if ($platform ne "win") {
    #---- Newer subprocess module back-ported from Python 3.2.
    # http://code.google.com/p/python-subprocess32/
    $landmark = "$komodoPythonUtilsDir/subprocess32.py";
    $consLocal->Command($landmark,
                   "subprocess32/setup.py",
                   qq(
                    cd %1:d && $siloedPython %1:f build $debugFlags
                    cd %1:d && $siloedPython %1:f install --skip-build --install-lib=$komodoPythonUtilsDir
                   ));
    $consLocal->DependsRecursive($landmark, "subprocess32", ("build", '\.svn'));
}
